import DICT from './pinyin';
export const toPinyin = (str = '') => str.replace(/./g, (c) => DICT[c] || c);


const getChromeVersion = () => {
    const userAgent = navigator.userAgent
    const chromeVersion = userAgent.match(/Chrome\/(\d+)\./)?.[1]
    return chromeVersion
}
export const CHROME_VERSION = Number(getChromeVersion())

if (CHROME_VERSION < 70) {
    throw new Error("Chrome 版本过低，请升级到 Chrome 70 以上")
}

let docBaseUrl: string = window["__DOC_BASE_URL__"] || "";
export const getDocBaseUrl = () => docBaseUrl;
export const setDocBaseUrl = (url: string) => docBaseUrl = url;

export const readFileAsText = (file: File): Promise<string> => {
    return new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.onload = () => {
            resolve(reader.result as string);
        };
        reader.onerror = reject;
        reader.readAsText(file);
    });
};